#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion
using System.Collections.Generic;

namespace Pandora.Console
{
    internal sealed class ConsoleManager : ConsoleBaseModule, IConsoleManager
    {
        private sealed class ConsoleWindowGroup : IConsoleWindowGroup
        {
            private readonly List<KeyValuePair<string, IConsoleWindow>> m_ConsoleWindows;

            private int m_SelectedIndex;

            private string[] m_WindowNames;

            public int WindowCount
            {
                get
                {
                    return m_ConsoleWindows.Count;
                }
            }

            public int SelectedIndex
            {
                get
                {
                    return m_SelectedIndex;
                }
                set
                {
                    m_SelectedIndex = value;
                }
            }

            public IConsoleWindow SelectedWindow
            {
                get
                {
                    if (m_SelectedIndex >= m_ConsoleWindows.Count)
                    {
                        return null;
                    }
                    return m_ConsoleWindows[m_SelectedIndex].Value;
                }
            }

            public ConsoleWindowGroup()
            {
                m_ConsoleWindows = new List<KeyValuePair<string, IConsoleWindow>>();
                m_SelectedIndex = 0;
                m_WindowNames = null;
            }

            public void Initialize(params object[] args)
            {
            }

            public void Shutdown()
            {
                foreach (KeyValuePair<string, IConsoleWindow> current in m_ConsoleWindows)
                {
                    current.Value.Shutdown();
                }
                m_ConsoleWindows.Clear();
            }

            public void OnEnter()
            {
                SelectedWindow.OnEnter();
            }

            public void OnLeave()
            {
                SelectedWindow.OnLeave();
            }

            public void OnUpdate(float elapseSeconds, float realElapseSeconds)
            {
                SelectedWindow.OnUpdate(elapseSeconds, realElapseSeconds);
            }

            public void OnDraw()
            {
            }

            private void RefreshConsoleWindowNames()
            {
                m_WindowNames = new string[m_ConsoleWindows.Count];
                int num = 0;
                foreach (KeyValuePair<string, IConsoleWindow> current in m_ConsoleWindows)
                {
                    m_WindowNames[num++] = current.Key;
                }
            }

            public string[] GetWindowNames()
            {
                return m_WindowNames;
            }

            public IConsoleWindow GetConsoleWindow(string path)
            {
                if (string.IsNullOrEmpty(path))
                {
                    return null;
                }
                int num = path.IndexOf('/');
                if (num < 0 || num >= path.Length - 1)
                {
                    return InternalGetConsoleWindow(path);
                }
                string name = path.Substring(0, num);
                string path2 = path.Substring(num + 1);
                ConsoleWindowGroup consoleWindowGroup = (ConsoleWindowGroup)InternalGetConsoleWindow(name);
                if (consoleWindowGroup == null)
                {
                    return null;
                }
                return consoleWindowGroup.GetConsoleWindow(path2);
            }

            public void RegisterConsoleWindow(string path, IConsoleWindow consoleWindow)
            {
                if (string.IsNullOrEmpty(path))
                {
                    throw new PdrException("路径错误");
                }
                int num = path.IndexOf('/');
                if (num >= 0 && num < path.Length - 1)
                {
                    string text = path.Substring(0, num);
                    string path2 = path.Substring(num + 1);
                    ConsoleWindowGroup consoleWindowGroup = (ConsoleWindowGroup)InternalGetConsoleWindow(text);
                    if (consoleWindowGroup == null)
                    {
                        if (InternalGetConsoleWindow(text) != null)
                        {
                            throw new PdrException("已呗注册了");
                        }
                        consoleWindowGroup = new ConsoleWindowGroup();
                        m_ConsoleWindows.Add(new KeyValuePair<string, IConsoleWindow>(text, consoleWindowGroup));
                        RefreshConsoleWindowNames();
                    }
                    consoleWindowGroup.RegisterConsoleWindow(path2, consoleWindow);
                    return;
                }
                if (InternalGetConsoleWindow(path) != null)
                {
                    throw new PdrException("被注册了，请检查");
                }
                m_ConsoleWindows.Add(new KeyValuePair<string, IConsoleWindow>(path, consoleWindow));
                RefreshConsoleWindowNames();
            }

            private IConsoleWindow InternalGetConsoleWindow(string name)
            {
                foreach (KeyValuePair<string, IConsoleWindow> current in m_ConsoleWindows)
                {
                    if (current.Key == name)
                    {
                        return current.Value;
                    }
                }
                return null;
            }
        }

        private readonly ConsoleWindowGroup _mConsoleWindowRoot;

        private bool m_ActiveWindow;

        internal override int Priority
        {
            get
            {
                return -1;
            }
        }

        public bool ActiveWindow
        {
            get
            {
                return m_ActiveWindow;
            }
            set
            {
                m_ActiveWindow = value;
            }
        }

        public IConsoleWindowGroup ConsoleWindowRoot
        {
            get
            {
                return _mConsoleWindowRoot;
            }
        }

        public ConsoleManager()
        {
            _mConsoleWindowRoot = new ConsoleWindowGroup();
            m_ActiveWindow = false;
        }

        internal override void Update(float elapseSeconds, float realElapseSeconds)
        {
            if (!m_ActiveWindow)
            {
                return;
            }
            _mConsoleWindowRoot.OnUpdate(elapseSeconds, realElapseSeconds);
        }

        internal override void Shutdown()
        {
            m_ActiveWindow = false;
            _mConsoleWindowRoot.Shutdown();
        }

        public void RegisterConsoleWindow(string path, IConsoleWindow consoleWindow, params object[] args)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new PdrException("路径错误");
            }
            if (consoleWindow == null)
            {
                throw new PdrException("window找不到");
            }
            _mConsoleWindowRoot.RegisterConsoleWindow(path, consoleWindow);
            consoleWindow.Initialize(args);
        }

        public IConsoleWindow GetConsoleWindow(string path)
        {
            return _mConsoleWindowRoot.GetConsoleWindow(path);
        }
    }
}
